const db = require('../db/index')

const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const config = require('../config')
const path = require('path')


exports.updateArtices = function(req, res) {
    console.log(req.body);
    const sql = 'select * from ev_articles where id<>? and (title=? or state=?)'
    db.query(sql, [req.body.id, req.body.title, req.body.state], (err, results) => {
        if (err) return res.cc(err)
        if (results.length === 2)
            return res.cc('文章标题与状态被占用，请更换后重试！')
        if (results.length === 1 && results[0].title === req.body.title)
            return res.cc('文章标题被占用，请更换后重试！')
        if (results.length === 1 && results[0].state === req.body.state)
            return res.cc('分类别名被占用，请更换后重试！')
        const sql1 = 'update ev_articles set ? where id=? '
        db.query(sql1, [req.body, req.body.id], function(err, rows) {
            if (err)
                return res.cc(err)
            if (rows.affectedRows !== 1) {
                return res.cc('更新文章分类失败！')
            } else {
                res.cc('更新文章成功', 0)
            }

        })

    })

}


exports.getArticles = function(req, res) {
    console.log(req.params.id);
    // const sql = 'select * from ev_articles where id=?'
    const sql = 'select a.*, c.name from ev_articles a left join ev_article_cate c on a.cate_id = c.id where a.id=?'

    db.query(sql, req.params.id, (err, results) => {
        if (err) return res.cc(err)
        if (results.length !== 1) return res.cc('获取文章分类数据失败！')
        res.send({
            status: 0,
            message: '获取文章分类数据成功！',
            data: results[0],
        })
    })

}


exports.deleteArticle = function(req, res) {
    console.log(req.params.id);
    const sql = 'select * from ev_articles  where id=? and is_delete=0'
    db.query(sql, req.params.id, function(err, rows) {
        if (err)
            return res.cc(err)
        if (rows.length !== 1) {
            console.log('id', req.user.id);
            return res.cc('文章不存在！')
        } else {
            const sql = 'update ev_articles  set is_delete=1 where id=?'
            db.query(sql, req.params.id, function(err, rows) {
                if (err)
                    return res.cc(err)
                if (rows.affectedRows !== 1) {
                    return res.cc('删除失败')
                } else {
                    res.cc('删除成功', 0)
                }
            })
        }
    })

}

exports.getList = function(req, res) {
    console.log(req.query);
    let { pagenum, pagesize, cate_id, state } = req.query
    pagesize = parseInt(pagesize)
    let recordnum = (parseInt(pagenum) - 1) * pagesize
    console.log(cate_id);
    const sql = 'select a.*, c.name from ev_articles a left join ev_article_cate c  on  a.cate_id=c.id where a.is_delete=0  limit ?,? '
    db.query(sql, [recordnum, pagesize], function(err, rows) {
        if (err)
            return res.cc(err)
        if (state !== '' && cate_id !== '') {
            const sql0 = ' select a.*, c.name from ev_articles a left join ev_article_cate c on  a.cate_id=c.id  where state=? and cate_id=? and  a.is_delete=0 limit ?,? '
            db.query(sql0, [state, cate_id, recordnum, pagesize], function(err, rows) {
                if (err)
                    return res.cc(err)
                const data = rows
                const sql2 = 'select count(*) as num from ev_articles a  left join ev_article_cate c on  a.cate_id=c.id where state=? and cate_id=?  and  a.is_delete=0 '
                db.query(sql2, [state, cate_id], function(err, rows) {
                    if (err) {
                        return res.cc(err)
                    }
                    res.send({
                        status: 0,
                        message: 'query ok',
                        data,
                        total: rows[0].num,
                    })

                })

            })
        } else if (cate_id !== '') {
            const sql0 = ' select a.*, c.name from ev_articles a  left join ev_article_cate c on  a.cate_id=c.id where cate_id=? and  a.is_delete=0 limit ?,? '
            db.query(sql0, [cate_id, recordnum, pagesize], function(err, rows) {
                if (err)
                    return res.cc(err)
                const data = rows
                const sql2 = 'select count(*) as num from ev_articles a  left join ev_article_cate c on  a.cate_id=c.id where cate_id=? and a.is_delete=0 '
                db.query(sql2, cate_id, function(err, rows) {
                    if (err) {
                        return res.cc(err)
                    }
                    res.send({
                        status: 0,
                        message: 'query ok',
                        data,
                        total: rows[0].num,
                    })

                })

            })
        } else if (state !== '') {
            const sql0 = ' select a.*, c.name from ev_articles  a left join ev_article_cate c on  a.cate_id=c.id where state=? and a.is_delete=0 limit ?,? '
            db.query(sql0, [state, recordnum, pagesize], function(err, rows) {
                if (err)
                    return res.cc(err)
                const data = rows
                const sql2 = 'select count(*) as num from ev_articles a  left join ev_article_cate c on  a.cate_id=c.id where state=? and a.is_delete=0 '
                db.query(sql2, state, function(err, rows) {
                    if (err) {
                        return res.cc(err)
                    }
                    res.send({
                        status: 0,
                        message: 'query ok',
                        data,
                        total: rows[0].num,
                    })

                })

            })
        } else {
            const data = rows
            const sql2 = 'select count(*) as num from ev_articles where is_delete=0 '
            db.query(sql2, req.body.cat_id, function(err, rows) {
                if (err) {
                    return res.cc(err)
                }
                res.send({
                    status: 0,
                    message: 'query ok',
                    data,
                    total: rows[0].num,
                })

            })
        }



    })
}




exports.addArticles = function(req, res) {
    // console.log(req.file);
    console.log('------------------');
    console.log(req.body);
    if (!req.file == null || req.file.fieldname !== 'cover_img')
        return res.cc('cover_img 是必须输入的！')
    const article_info = {
        ...req.body,
        cover_img: path.join('/uploads/', req.file.filename),
        pub_date: new Date(),
        author_id: req.user.id,

    }
    console.log(article_info);
    const sql = 'insert into ev_articles set ? '
    db.query(sql, article_info, function(err, result) {
        if (err) {
            res.cc(err)
        }
        if (result.affectedRows !== 1)
            return res.cc('insert failed')
        res.cc('文章发布成功', 0)
    })

}